package com.ocm.core.dao.jpa;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.ocm.core.dao.SecurityDao;
import com.ocm.core.domain.UserDetail;
import com.ocm.core.domain.UserDetailType;

@Repository("securityDao")
public class JpaSecurityDao extends JpaGenericDao<UserDetail> implements SecurityDao {

   @Autowired
   public JpaSecurityDao(@Qualifier("ocmEntityManagerFactory")EntityManagerFactory entityManagerFactory) {
      super(UserDetail.class);
      super.setEntityManagerFactory(entityManagerFactory);
   }

   @SuppressWarnings("unchecked")
   public List<UserDetail> findUserAll() {
      Map<String, Object> paras = new HashMap<String, Object>();
      paras.put(UserDetail.PARA_TYPE, UserDetailType.USER);
      List<UserDetail> users = this.getJpaTemplate().findByNamedQueryAndNamedParams(UserDetail.FIND_ALL, paras);
      return users;
   }

   @SuppressWarnings("unchecked")
   public List<UserDetail> findGroupAll() {
      Map<String, Object> paras = new HashMap<String, Object>();
      paras.put(UserDetail.PARA_TYPE, UserDetailType.GROUP);
      List<UserDetail> groups = this.getJpaTemplate().findByNamedQueryAndNamedParams(UserDetail.FIND_ALL, paras);
      return groups;
   }

   @SuppressWarnings("unchecked")
   public List<UserDetail> findAll(Long[] secIds) {
      List<UserDetail> users = new ArrayList<UserDetail>();
      if (secIds.length > 0){
         List<Long> listId = Arrays.asList(secIds);
         Map<String, Object> paras = new HashMap<String, Object>();
         paras.put(UserDetail.PARA_IDs, listId);
         users = this.getJpaTemplate().findByNamedQueryAndNamedParams(UserDetail.FIND_IDs, paras);
      }
      return users;
   }

   @SuppressWarnings("unchecked")
   public List<UserDetail> findAll(Long[] secIds, Long userId) {
      List<UserDetail> users = new ArrayList<UserDetail>();
      if (secIds.length > 0){
         Map<String, Object> paras = new HashMap<String, Object>();
         List<Long> listId = Arrays.asList(secIds);
         paras.put(UserDetail.PARA_IDs, listId);
         paras.put(UserDetail.PARA_USERID, userId);
         users = this.getJpaTemplate().findByNamedQueryAndNamedParams(UserDetail.FIND_BY_GROUPIDS_USERID, paras);
      }
      return users;
   }

   @SuppressWarnings("unchecked")
   public List<UserDetail> findListByGroupName(String name) {
      Map<String, Object> paras = new HashMap<String, Object>();
      paras.put(UserDetail.PARA_NAME, name.toLowerCase()+"%");
      paras.put(UserDetail.PARA_TYPE, UserDetailType.GROUP);
      List<UserDetail> groups = this.getJpaTemplate().findByNamedQueryAndNamedParams(UserDetail.FIND_LIKENAME_TYPE, paras);
      return groups;
   }

   @SuppressWarnings("unchecked")
   public List<UserDetail> findListByUsername(String name) {
      Map<String, Object> paras = new HashMap<String, Object>();
      paras.put(UserDetail.PARA_NAME, name.toLowerCase()+"%");
      paras.put(UserDetail.PARA_TYPE, UserDetailType.USER);
      List<UserDetail> users = this.getJpaTemplate().findByNamedQueryAndNamedParams(UserDetail.FIND_LIKENAME_TYPE, paras);
      return users;
   }

   @SuppressWarnings("unchecked")
   public UserDetail findByGroupName(String name) {
      Map<String, Object> paras = new HashMap<String, Object>();
      paras.put(UserDetail.PARA_NAME, name);
      paras.put(UserDetail.PARA_TYPE, UserDetailType.GROUP);
      List<UserDetail> groups = this.getJpaTemplate().findByNamedQueryAndNamedParams(UserDetail.FIND_NAME, paras);
      if (groups.size()>0)
         return groups.get(0);
      return null;
   }
   
   @SuppressWarnings("unchecked")
   public UserDetail findByUsername(String name) {
      Map<String, Object> paras = new HashMap<String, Object>();
      paras.put(UserDetail.PARA_NAME, name);
      paras.put(UserDetail.PARA_TYPE, UserDetailType.USER);
      List<UserDetail> users = this.getJpaTemplate().findByNamedQueryAndNamedParams(UserDetail.FIND_NAME, paras);
      if (users.size()>0)
         return users.get(0);
      return null;
   }

   @SuppressWarnings("unchecked")
   public List<UserDetail> findListByName(String name) {
      Map<String, Object> paras = new HashMap<String, Object>();
      paras.put(UserDetail.PARA_NAME, name.toLowerCase()+"%");
      List<UserDetailType> userDetailTypes = new ArrayList<UserDetailType>();
      userDetailTypes.add(UserDetailType.USER);
      userDetailTypes.add(UserDetailType.GROUP);
      paras.put(UserDetail.PARA_TYPE, userDetailTypes);
      List<UserDetail> users = this.getJpaTemplate().findByNamedQueryAndNamedParams(UserDetail.FIND_LIKENAME, paras);
      return users;
   }

   @SuppressWarnings("unchecked")
   public List<UserDetail> findListByGroupNames(String[] names) {
      List<UserDetail> users = new ArrayList<UserDetail>();
      if (names != null && names.length > 0){
         Map<String, Object> paras = new HashMap<String, Object>();
         List<String> listName = Arrays.asList(names);
         paras.put(UserDetail.PARA_NAME, listName);
         paras.put(UserDetail.PARA_TYPE, UserDetailType.GROUP);
         users = this.getJpaTemplate().findByNamedQueryAndNamedParams(UserDetail.FIND_NAMEs, paras);
      }
      return users;
   }
}
